from django.db import models

from user.models import UserInfo
from goods.models import Goods


# Create your models here.

class Base(models.Model):
    # 是否注销
    is_delete = models.BooleanField(default=False)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)
    
    class Meta:
        abstract = True


class Order(Base):
    ORDER_STATUS = ((1, '待付款'), (2, '待收货'), (3, '待评价'))
    good = models.ForeignKey(to='ShoppingCar', on_delete=models.DO_NOTHING, verbose_name='商品')
    money = models.DecimalField(max_length=10, max_digits=10, decimal_places=2, verbose_name='总价钱')
    order_state = models.IntegerField(choices=ORDER_STATUS, default=1, verbose_name='订单状态')
    order_uuid = models.CharField(max_length=254, verbose_name='订单编号')
    
    class Meta:
        verbose_name = '订单'
        verbose_name_plural = verbose_name


class ShoppingCar(Base):
    # 用户
    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE, verbose_name='用户')
    # 商品信息
    goods = models.ManyToManyField(to=Goods, verbose_name='商品', db_constraint=False)
    goods_num = models.IntegerField(verbose_name='商品个数', null=True)
    
    def __str__(self):
        return self.user.username + '的购物车'
    
    class Meta:
        verbose_name_plural = '购物车表'
    
    def goods_info(self):
        goods = []
        for good in self.goods.all():
            goods.append(
                {
                    'title': good.title,
                    'desc': good.desc,
                    'price': good.sale_price,
                    'number': self.goods_num
                }
            )
        return goods
